package de.previsor.workflow.control;


import de.previsor.workflow.MainApp;
import de.previsor.workflow.control.DBConnect;
import java.net.URL;
import java.util.ArrayList;
import java.util.ResourceBundle;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.image.ImageView;




public class RootController implements Initializable{
    @FXML public Button btn_workflow;
    @FXML public Button btn_asset;
    @FXML public Button btn_search;
    @FXML public ImageView img_search;
    @FXML public TextField inp_search;   
    @FXML public TextArea txt_solution;
    @FXML public Button btn_hide;
    
    DBConnect c = new DBConnect();
    
    
    
   
    
    
    @FXML 
    public void pressAsset(ActionEvent e)
    {
        System.out.println("Press Asset");
        MainApp.showPrevisorUiAsset();
    }
    
    @FXML 
    public void pressWorkflow(ActionEvent e)
    {
        System.out.println("Press Workflow");
       
        MainApp.showPrevisorUiWorkflow();
    }  
    
    @FXML
    public void search(ActionEvent e) 
    {
        //public ArrayList<String[]> Search(String keyword, String type)
        boolean foundC = false;
        boolean foundT = false;
        boolean foundA = false;
        boolean foundB = false;
        boolean foundO = false;
        
        System.out.println("Suche starten...");
        String sPhrase = inp_search.getText();
        String results ="";
        
        ArrayList <String[]> AssetSolutions = new ArrayList<String[]>();
        ArrayList <String[]> TaskSolutions = new ArrayList<String[]>();
        
        
        //Allgemeine Suchen
        
        AssetSolutions = c.Search(sPhrase, "asset");
        TaskSolutions = c.Search(sPhrase, "task");
        if(AssetSolutions.isEmpty() == false || TaskSolutions.isEmpty() == false)
        {
            foundO = true;
        }
        //Firmen durchsuchen
        if(c.getCompanyID(sPhrase)==0)
        {
            foundC = false;
            System.out.println(c.getCompanyID(sPhrase));
            System.out.println("Keine Ergebnisse gefunden!");
        }
        if(c.getCompanyID(sPhrase)>0)
        {
            foundC = true;
            System.out.println(c.getCompanyID(sPhrase));
            System.out.println("Ergebnisse gefunden!");
        }
        
        //Aufträge durchsuchen
        
        
        //Assets durchsuchen
        if(c.getAssetID(sPhrase)==0)
        {
            foundA = false;
            System.out.println(c.getAssetID(sPhrase));
            System.out.println("Keine Ergebnisse gefunden!");
        }
        if(c.getAssetID(sPhrase)>0)
        {
            foundA = true;
            System.out.println(c.getAssetID(sPhrase));
            System.out.println("Ergebnisse gefunden!");
        }
        
        //Bereiche durchsuchen
        if(c.getSectionID(sPhrase)==0)
        {
            foundB = false;
            System.out.println(c.getSectionID(sPhrase));
            System.out.println("Keine Ergebnisse gefunden!");
        }
        if(c.getSectionID(sPhrase)>0)
        {
            foundB = true;
            System.out.println(c.getSectionID(sPhrase));
            System.out.println("Ergebnisse gefunden!");
        }
        
        if(foundA || foundB ||foundC || foundT || foundO)
        {
            txt_solution.setOpacity((double) 0.8);
            txt_solution.setVisible(true);
            btn_hide.setVisible(true);
            
            txt_solution.setText("Ihre suche mit dem Begriff " + sPhrase + " ergab \nfolgende Ergebnisse: \n");
            if(foundA)
            {
                txt_solution.setText(txt_solution.getText()+"Asset: "+ sPhrase +"\nID: "+ c.getAssetID(sPhrase));
                
            }
            if(foundB)
            {
                txt_solution.setText(txt_solution.getText()+"1 Asset mit diesem Namen");
            }
           
                int i = 0;
                while(i<AssetSolutions.size())
                {
                    String[] resultArray = AssetSolutions.get(i);
                    results = results + " " + resultArray[1] + " Pfad : " + resultArray[2]+"\n";
                    System.out.println(results);
                    i++;
                }       
                i = 0;
                while(i<TaskSolutions.size())
                {
                    String[] resultArray = TaskSolutions.get(i);
                    results = results + "Auftragsname : " + resultArray[1] + " Firma : " + resultArray[2] + "\n";
                    System.out.println(results);
                    i++;
                }  
           
            txt_solution.setText("Die Suche nach "+ sPhrase + " ergab folgende Ergebnisse: \n" +results);
            
        }
        
        
    }
    @FXML
    public void hideSearch(ActionEvent e) 
    {
    txt_solution.setVisible(false);
    btn_hide.setVisible(false);
    }

    
    
    
    public void initialize(URL url, ResourceBundle rb) {
       //tester();
    }
    public static void hideHeaderBtn(){
        
    }
    
    
    public void tester(){
        int i = 1;
        System.out.println("!!!!!" + i + "tester ausgeführt!!!!!") ;
        i++;
        
        btn_workflow.setVisible(false);
        btn_asset.setVisible(false);
    }
    
   

}